/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.OrderStatusHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/12/2 16:27
 */
public interface OrderStatusHistoryRepository extends JpaRepository<OrderStatusHistory, Long> {
    /**
     * 按通知状态获取状态记录
     *
     * @param notified
     * @return
     */
    List<OrderStatusHistory> findAllByCustomerNotifiedOrderByDateAdded(Boolean notified);

    /**
     * 查询所有未通知的状态记录
     *
     * @return
     */
    @Query(value = "SELECT * FROM ORDER_STATUS_HISTORY WHERE CUSTOMER_NOTIFIED IS  NULL  OR CUSTOMER_NOTIFIED = 0 ", nativeQuery = true)
    List<OrderStatusHistory> findAllUnnotifiedHistories();

    /**
     * 查询订单某状态的时间
     * @param orderId
     * @param status
     * @return
     */
    @Query(value = "SELECT DATE_ADDED FROM ORDER_STATUS_HISTORY WHERE ORDER_ID =?1 AND STATUS =?2 LIMIT 1",nativeQuery = true)
    Date getTimeOfOrder(Long orderId, String status);

    /**
     * 查询发货时间
     * @param orderId
     * @param orderType
     * @return
     */
    @Query(value = "SELECT DATE_ADDED from ORDER_STATUS_HISTORY WHERE ORDER_ID =?1 AND STATUS ='SHIPPED' AND ORDER_TYPE = ?2 order by ORDER_STATUS_HISTORY_ID  LIMIT 1",nativeQuery = true)
    Date getShippedTime(Long orderId,String orderType);

}
